<?php

namespace app\common\model;

use think\Db;
use think\Model;

class Skill extends Model
{
    public function skillForm()
    {
        return $this->hasMany(SkillForm::class,'skill_id','skill_id');
    }

    public function getList($where = []){
        return $this
            ->where($where)
            ->order('sort')
            ->paginate('',false,['query'=>request()->param()]);
    }

    public function getDetail($map = [])
    {
        $data =  $this->where($map)->field('skill_id,skill_name,img,is_fast,form_id,grade,spec,tag')->cache(15)->find();

        if ($data){

            $form['form_id'] = ['in',$data['form_id']];
            $form['status']  = 1;
            $data['form'] = Db::name('skill_form')->field('form_id,form_name')->where($form)->cache(15)->select();

            $data['tags'] = getSkillTags($data['tag']);

            $data['grade'] = explode(',',$data['grade']);
        }

        return $data;

    }

    public function getRows($map = [])
    {
        return $this->where($map)->order('sort')->field('skill_id,skill_name,skill_img')->select();
    }

    public function getSkillRows($map = [], $skill = [], int $user_id) :array
    {
        $check  = SkillApply::where(['user_id' => $user_id, 'status'=> 0])->column('skill_id');
        $reject = SkillApply::where(['user_id' => $user_id, 'status'=> 1])->column('skill_id');

        $skills =  $this->where($map)
            ->whereNotIn('skill_id', $skill)
            ->order('sort')
            ->field('skill_id,skill_name,status,skill_img,type')
            ->select();

        foreach ($skills as $skill)
        {
            $skill['status'] = -1;
            if(in_array($skill->skill_id, $check))
                $skill['status'] = 0;
            if(in_array($skill->skill_id, $reject))
                $skill['status'] = 1;
        }
        return $skills;
    }

    public function getCateList($map = [])
    {

        return $this->where($map)
            ->order('sort')
            ->field('skill_id,skill_name,status,skill_img')
            ->select();

    }


}
